package com.catal.framework.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.acegisecurity.util.StringSplitUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;

import com.catal.framework.sequence.Sequence;

public class WebContextUtil{
	private static Log log = LogFactory.getLog(WebContextUtil.class.getName());
	
	private static ApplicationContext context = null;
	
	private static final String SPRING_FILE = "/*.spring.xml";
	
	private static final String MVC_FILE = "/*.mvc.xml";

	private static final String FUNC_FILE = "/**/*.menu.xml";

	private static final String DWR_FILE = "/*.dwr.xml";
	
	public static List<Map<String,Object>> allMenu;
	
	public static Map<String, Map> opers;
	
	public static void init() {
		context = new ClassPathXmlApplicationContext("classpath*:/conf/*.spring.xml");
	}
	
	/**
	 * 取得容器中的jdbcTemplate bean
	 */
	public static JdbcTemplate getJdbcTemplate() {
		ApplicationContext c = WebContextUtil.getContext();
		DataSource ds = (DataSource) c.getBean("dataSource");
		JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
		return jdbcTemplate;
	}

	public static ApplicationContext getContext() {
		return context;
	}

	public static void setContext(ApplicationContext context) {
		WebContextUtil.context = context;
	}
	
	public static boolean isSuperAdmin(String username){
		if("admin".equals(username)){
			return true;
		}
		return false;
	}
	
	/**
	 * 序列号实例
	 * @return
	 */
	public static Sequence getDefaultSequence(){
		return (Sequence) context.getBean("DefaultSequence");
	}
	
	/**
	 * 根据ID生成自增序列号
	 * @param seqDefid
	 * @return
	 */
	public static String generateSequence(String seqDefid){
		return getDefaultSequence().generate(seqDefid);
	}
	
	/**
	 * 根据参数path读取propertyes配置文件，path以'/'开始（即classpath的根）。
	 */
	public static Map<String, String> loadProperties(String path) {

		Map<String, String> map = new LinkedHashMap<String, String>();
		try {
			InputStream is = WebContextUtil.class.getResourceAsStream(path);
			Reader w = new InputStreamReader(is, "UTF-8");
			BufferedReader br = new BufferedReader(w);
			String line = br.readLine();
			while (line != null) {
				line = line.trim();
				if (!line.startsWith("#")) {
					String name = StringSplitUtils.substringBeforeLast(line,
							"=");
					String value = StringSplitUtils.substringAfterLast(line,
							"=");
					if (StringUtils.hasLength(name)
							&& StringUtils.hasLength(value)) {
						map.put(name.trim(), value.trim());
					}
				}
				line = br.readLine();
			}
			return map;

		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
}
